<?php
 //============================================================================
 // ____________________________PLIGG TEAM ADDON_______________________________
 //
// This CLASS was created by AnAlienHolakres3 ||| XXXXX  XX     XX XXXX   XXXX
// (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
// @last_update                    08/03/2008 ||| XX     XX     XX X  XX  X  XX
// @version                              0.91 ||| XX     XXXXXX XX XXXXX  XXXXX
//
// This file can be used by Pligg community.
//=============================================================================

// Class TeamShoutbox contains general information about team shoutbox


class TeamShoutbox {
 
    private $id =0;    
    private $team_id=0;    
    private $creator_id=0;
    private $setup_date='';       
    private $message='';        
    private $shoutbox_data;
    private $messages_count;
    
    private $read = true;
  
    // settings
    // when this interval is reached, script checks if in database is the same count
    // of posted team shoutbox messages, if it is higher then rest of this messages
    // is loaded
    private $refresh_interval = 15;                                                                           
    
    //============================================================================
    //  Function read_team_shoutbox() gets all shoutbox data (messages from users - array)
    //  for specific team  (everytime when whole page is reloaded)
    //============================================================================      
    
    function read_team_shoutbox() {
        global $db;
        
        
      $team_id = $this->team_id;       
      $shoutbox_data = $db->get_results("SELECT shoutbox_id,creator_id, message, setup_date,user_login FROM " . table_shoutbox . 
                        " JOIN " . table_users . " ON " . table_users . ".user_id=" . table_shoutbox . ".creator_id
                          WHERE team_id=$team_id ORDER BY setup_date DESC");                                      
      
  
          if ($shoutbox_data) {   
              $this->shoutbox_data = $shoutbox_data;
               return true;    
           }
              
        $this->read = false;
        return false;
    } 
    
    //============================================================================
    //  Function read_rest_shouts() gets all shouts which user has not loaded yet
    //  for specific team  shout
    //============================================================================      
    
    function read_rest_shouts() {
        global $db;
       
       $team_id = $this->team_id;        
       $current_count = $_POST["messages_count"];
       $database_count = $this->get_messages_count();
       $update_count = $database_count - $current_count;
         
      $shoutbox_data = $db->get_results("SELECT shoutbox_id,creator_id, message, setup_date,user_login FROM " . table_shoutbox . 
                        " JOIN " . table_users . " ON " . table_users . ".user_id=" . table_shoutbox . ".creator_id
                          WHERE team_id=$team_id ORDER BY setup_date DESC LIMIT $update_count");                                      
      
  
          if ($shoutbox_data) {   
              $this->shoutbox_data = $shoutbox_data;
               return true;    
           }
              
        $this->read = false;
        return false;
    }
    //============================================================================
    //  Function read_messages_count() read messages count for specific team 
    //  shoutbox in database
    //============================================================================    
    
    function read_messages_count() {
     global $db; 
       $team_id  = $this->team_id;
       
        $count=$db->get_var("SELECT count(shoutbox_id) FROM " . table_shoutbox ." WHERE team_id=$team_id");
        $this->messages_count =$count;
        
    }
    
    //============================================================================
      //  Function fill_print() assign shout box smartys
      //============================================================================
      
      function fill_print()
      {
          global $main_smarty,$db;
          
          $shoutbox_data = $this->shoutbox_data;                         
          $team = new Team;
          $language = $team->get_language();
                        
          if ($shoutbox_data) {     
                
                  foreach ($shoutbox_data as $shout) {
                                                    
                  $i++;
                  if ($i % 2 == 0){
                  $shout_row[] =2; 
                  $last_shout=1;
                  
                  } else {
                  $shout_row[] =1;     
                  $last_shout=2;
                  }                  
                  
                  $shoutbox_ids[] = $shout->shoutbox_id;
                  $shoutbox_messages[] = $shout->message;
                  $shoutbox_creator_names[] = $shout->user_login;
                  
                  
                  if ($language == "en"){
                  $shoutbox_times[] = $shout->setup_date;
                  
                  } else {                                       
                  $shoutbox_times[] = date('d.m.Y H:i',strtotime($shout->setup_date));                                       
                  
                  }
                  
                 }                 
                 
                 $main_smarty->assign('last_shout',$last_shout); 
                 $main_smarty->assign('shout_row',$shout_row);
                 $main_smarty->assign('team_shoutbox_creator_names', $shoutbox_creator_names); 
                 $main_smarty->assign('team_shoutbox_ids', $shoutbox_ids);
                 $main_smarty->assign('team_shoutbox_messages', $shoutbox_messages);                                  
                 $main_smarty->assign('team_shoutbox_times', $shoutbox_times);
                
                                                   
             } else {
                $main_smarty->assign('last_shout',0);  
             }
              $this->read_messages_count();
              $main_smarty->assign('team_shoutbox_messages_count',$this->get_messages_count());                   
              $main_smarty->assign('shoutbox_refresh_interval',$this->refresh_interval);                   
      } 
    
    //============================================================================
    //  Function store() saves shoutbox to database
    //============================================================================
    
    function store() {
        global $db;
    
        if(!$this->setup_date) $this->setup_date=time();              
                
        $team_id           = $this->team_id;
        $message           = $this->message;
        $setup_date        = $this->setup_date;
        $creator_id      = $this->creator_id;
        $message           = $this->message;
                     
        if($this->id===0) {   
           $sql = ("INSERT INTO " . table_shoutbox . " (team_id,
           setup_date, creator_id ,message) VALUES ($team_id,FROM_UNIXTIME($setup_date), 
          $creator_id, '$message')");        
       
       } 
        if($db->query($sql)){ 
              return true;     
              
        }     
          return false;
       }                                          
    
      //==============================================================================
      // Function save_shoutbox_values()  sets values  and saves values to database           
      //==============================================================================  
      
      function save_shoutbox_values()
      {      global $current_user;
         
         $this->set_shoutbox_team_id($_POST['team_id']);           
         $this->set_shoutbox_creator_id($_POST['user_id']);
         $this->set_shoutbox_message($_POST['shoutbox_message']);
         
          //store to database
          if (!$this->store()) { 
              //cannot save to dabase
              return false;
          }
          return true;
      }
      
      //==============================================================================
      // Function is_update_neccessary() returns true if  team shoutbox message count
      // in database is gerater than current messages count
      //==============================================================================  
      
      function is_update_neccessary() 
      {      global $db;
         
         $team_id = $this->team_id;
         
         $current_count = $_POST["messages_count"];
         $database_count = $this->get_messages_count();
                 
          
          if ($current_count >= $database_count) { 
          
              return false;
          }
          
          return true;
      }
    
 
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  GETTING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     
      //==============================================================================
      //  Function get_shoutbox_data()  returns array of team shoutbox data
      //=============================================================================
      function get_shoutbox_data()
      {
          return $this->shoutbox_data;
      }
      
      //==============================================================================
      //  Function get_messages_count()  returns count of team shoutbox messages in db
      //=============================================================================
      function get_messages_count() 
      {  
          $messages_count = $this->messages_count;
          return $messages_count;            
      }
      
      
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  CHECKING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      
  
    
    //========================================================================
    // Function check_message_length() checks task name length
    // @return true if name length is correct otherwise false
    //========================================================================    

        function check_message_exist() {
        $team = new Team;
        
        if (!$_POST["shoutbox_message"] || $team->strlen_utf8(trim($_POST["shoutbox_message"]))==0) {            
            return false;
            }  
            return true;            
    }
    
    //========================================================================
    // Function check_messages_count() checks if messages count is digit
    // @return true if it is otherwise false
    //========================================================================    

        function check_messages_count() {
                
        if (!ctype_digit(trim($_POST["messages_count"]))) {       
                return false;
        }
                return true;
    } 

        //========================================================================
    // Function check_last_shout() checks if messages count is digit
    // @return true if it is otherwise false
    //========================================================================    

        function check_last_shout() {
                
        if (!ctype_digit(trim($_POST["last_shout"]))) {
               return false;
        }
               return true;
    }
    

 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  SETTING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   
    //============================================================================
    //  Function set_shoutbox_id() sets shoutbox id
    //  @param $shoutbox_id
    //============================================================================      
    function set_shoutbox_id($shoutbox_id) {
     $this->id = $shoutbox_id;
    } 
    
    //============================================================================
    //  Function set_shoutbox_team_id() sets shoutbox team id
    //  @param $shoutbox_team_id
    //============================================================================      
    function set_shoutbox_team_id($team_id) {
     $this->team_id = $team_id;
    }
    
      //============================================================================
    //  Function set_shoutbox_creator_id() sets shoutbox  creatorid
    //  @param $creator_id
    //============================================================================      
    function set_shoutbox_creator_id($creator_id) {
        global $db;
     $this->creator_id = $creator_id;
    } 
  
    //============================================================================
    //  Function set_shoutbox_message() sets shoutbox message
    //  @param $creator_name
    //============================================================================      
    function set_shoutbox_message($message) {
        global $db;
     $this->message = $db->escape(htmlspecialchars($this->urlRawDecode($message))); 
     
    } 
     function urlRawDecode($raw_url_encoded)
{
    # Hex conversion table
    $hex_table = array(
        0 => 0x00,
        1 => 0x01,
        2 => 0x02,
        3 => 0x03,
        4 => 0x04,
        5 => 0x05,
        6 => 0x06,
        7 => 0x07,
        8 => 0x08,
        9 => 0x09,
        "A"=> 0x0a,
        "B"=> 0x0b,
        "C"=> 0x0c,
        "D"=> 0x0d,
        "E"=> 0x0e,
        "F"=> 0x0f
    );
       
    # Fixin' latin character problem
        if(preg_match_all("/\%C3\%([A-Z0-9]{2})/i", $raw_url_encoded,$res))
        {
            $res = array_unique($res = $res[1]);
            $arr_unicoded = array();
            foreach($res as $key => $value){
                $arr_unicoded[] = chr(
                        (0xc0 | ($hex_table[substr($value,0,1)]<<4))
                       | (0x03 & $hex_table[substr($value,1,1)])
                );
                $res[$key] = "%C3%" . $value;
            }

            $raw_url_encoded = str_replace(
                                    $res,
                                    $arr_unicoded,
                                    $raw_url_encoded
                        );
        }
       
        # Return decoded  raw url encoded data
        return rawurldecode($raw_url_encoded);
}
  
}
?>

